package com.daoeping.dao;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.sql.ANSICaseFragment;
import org.json.JSONArray;
import org.springframework.transaction.annotation.Transactional;

import com.daoeping.data.Bzn_question;
import com.daoeping.domain.JsonData;
import com.daoeping.domain.Page;
import com.daoeping.domain.TableData;
import com.daoeping.util.ConverntJsonData;
import com.opensymphony.xwork2.ActionContext;





public class Bzn_answerDAO extends AllDaoSupport {
	
	public JsonData findAllBzn_answerByPage (Page page, TableData tableData, Bzn_question bzn_question) {
		try {
			StringBuilder sb1 = new StringBuilder();
			StringBuilder sb2 = new StringBuilder();
			sb1.append("select ans.content CONTENT, user.username NAME, ans.id ANSID, user.id USERID, temp.average AVERAGE , temp.number NUMBER from " +
					"(select anscomm.answer_id as id , avg(anscomm.rank ) as average , count(anscomm.answer_id ) as number from bzn_answercomm anscomm  group by  anscomm.answer_id ) temp " +
					"right join  bzn_answer ans on ans.id = temp.id left join usr_user user on ans.user_id = user.id " +
					"left join bzn_question ques on ques.id = ans.question_id "+
					"where ques.id = '"+bzn_question.getId()+"' order by AVERAGE desc");
			sb2.append("select count(bzn) from Bzn_answer bzn where bzn.bzn_question.id = '"+bzn_question.getId()+"'");
			List<Object[]> resultlist = (List<Object[]>) this.getPageList(page, sb1.toString());
			JSONArray json = new ConverntJsonData().getJsonData(resultlist);
			JSONArray newJson = new JSONArray();
			newJson.put(this.getNum(page, sb2.toString()));
			for (int i = 0; i < json.length(); i++) {
				/*Object[]objects = (Object[]) json.get(i);
				ActionContext.getContext().getSession().put(objects[2]+"_score", objects[4]+"<"+objects[5]);
				System.out.println(ActionContext.getContext().getSession().get(objects[2]+"_score"));
				*/newJson.put(json.get(i));
			}
			JsonData jsonData = new JsonData();
			jsonData.setJsonData(newJson.toString());
			System.out.println(newJson);
			return jsonData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	public boolean delBzn_answerByQuestionId(Bzn_question bzn_question){
		return updateDataByHQL("delete from Bzn_answer ans where ans.bzn_question.id =" +bzn_question.getId());
	}
	
	@Transactional
	public Object getPageList(Page page, String sql) {
		int temp = (page.getPage() - 1) * page.getPageSize();
		return pageBzn_answerBySQL(sql , temp ,page.getPageSize() );
		
	}
	@Transactional
	public Object pageBzn_answerBySQL(String hql, int offSet, int pageSize) {
		System.out.println("operation:select,hql:"+hql);
		log.debug("operation:select,hql:"+hql+",offSet:"+offSet+",pageSize:"+pageSize);
		try {
			return sessionFactory.getCurrentSession().createSQLQuery(hql).addScalar("CONTENT").addScalar("NAME").addScalar("ANSID").
					addScalar("USERID").addScalar("AVERAGE").addScalar("NUMBER").setFirstResult(offSet).setMaxResults(pageSize).list();
		} catch (Exception e) {
			return false;
		}
		
	}
}